package b2.scene.fight.skills
{
	import b2.config.EnemySkillMovieConfig;
	import b2.scene.fight.PlayState;
	import b2.scene.fight.TankSprite;
	
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class AreaHarm extends BasicSkill
	{
		protected var areaArgs:Array;
		//技能动画参数
		private var movieArgs:Array;
		//skillArgs
		public function AreaHarm(tankId:int, tankSide:int ,level:int, skillArgs:Object)
		{
			if(skillArgs["areas"] != null)
				areaArgs = skillArgs["areas"] as Array;
			if (skillArgs["areas"] != null)
				movieArgs = skillArgs["movies"] as Array;
			super(tankId, tankSide ,level, skillArgs);
		}
		
		public override function discharge(x:int, y:int, direction:int):void
		{
			var skillArea:SkillArea = createMySkillArea(x, y, direction);
			
			playSkillMovie(x, y, direction);
			
			hitOtherTank(tankId, skillArea);
			
     		super.discharge(x, y, direction);
		}

		protected function createMySkillArea(x:int, y:int, direction:int):SkillArea
		{
			var skillArea:SkillArea = new SkillArea(x, y, direction);
			for (var i:int = 0; i < areaArgs.length; i++)
			{
				if (areaArgs[i]["areaIndex"] != null && areaArgs[i]["areaIndex"] == 1)
					skillArea.addCrossArea(areaArgs[i].front, areaArgs[i].back, areaArgs[i].left, areaArgs[i].right);
				else
					skillArea.addArea(areaArgs[i].front, areaArgs[i].back, areaArgs[i].left, areaArgs[i].right);
			}
			return skillArea;
		}
		
		protected function playSkillMovie(x:int, y:int, direction:int):void
		{
			var moviePosition:SkillPosition = new SkillPosition(x, y, direction);
			var tank:TankSprite = getPlayState().tanks[tankId];
			
			//添加动画播放地点
			for (var i:int = 0; i < movieArgs.length; i++)
			{
				var skillMovieConfig:EnemySkillMovieConfig = movieArgs[i];
				var point:Point = moviePosition.transPoint(skillMovieConfig.x, skillMovieConfig.y);
				getPlayState().playFixMovie(skillMovieConfig.movieIndex, int(point.x), int(point.y), (direction + skillMovieConfig.direction) % 4, true);
			}
		}
		
		protected function hitOtherTank(ownerId:int, skillArea:SkillArea):void
		{
			for (var tankIdStr:String in getPlayState().tanks)
			{
				var otherTank:TankSprite = getPlayState().tanks[tankIdStr];
				if (otherTank != null && (tankId > 0 && otherTank.tankId < 0 || tankId < 0 && otherTank.tankId > 0))
				{
					if (skillArea.intersects(new Rectangle(otherTank.x + 2, otherTank.y + 2, PlayState.TILE_WIDTH - 4, PlayState.TILE_WIDTH - 4)))
					{
						onHit(otherTank);
					}
				}
			}
		}
		
		protected function onHit(tankOnHit:TankSprite):void
		{
			var areaDemage:int = level < skillArgs.areaDemage.length ? skillArgs.areaDemage[level] : skillArgs.areaDemage[0] * level;
			getPlayState().updateTankStatus(tankOnHit.tankId, tankId, tankOnHit.currentHp, -areaDemage, 0, 0, true, true);
			if (skillArgs.effectIndex != null && skillArgs.effectIndex != 0)
			{
				getPlayState().addTankEffect(tankOnHit.tankId, tankId,skillArgs.effectIndex, skillArgs.effectDuration[0], 
					skillArgs.effectValue[0], skillArgs.effectValue2[0], true);
			}
		}
	}
}